package ch.ffhs.radarpp.managers;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import ch.ffhs.radarpp.R;
import ch.ffhs.radarpp.misc.BaseActivity;
/**
 * 
 * @author Beda
 *
 */
public class ActualLocationManager implements LocationListener {

	private android.location.LocationManager locationManager;
	private static ActualLocationManager sharedInstance;
	
	private Location lastLocation;

	public static ActualLocationManager getInstance()
	{
		if(sharedInstance == null)
		{
			sharedInstance = new ActualLocationManager();
		}
		
		return sharedInstance;
	}
	
	public Location getLastLocation()
	{
		return lastLocation;
	}
	
	public  void stopObservingActualPosition()
	{
		
			locationManager.removeUpdates(this);
		
	}
	
	public  void startObservingActualPosition(Context context)
	{
		
		locationManager = (android.location.LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		
		if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {

			locationManager.requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER, 5000, 10, this);
			locationManager.requestLocationUpdates(android.location.LocationManager.NETWORK_PROVIDER, 5000, 200, this);
			locationManager.requestLocationUpdates(android.location.LocationManager.PASSIVE_PROVIDER, 5000, 200, this);

			lastLocation = locationManager.getLastKnownLocation(android.location.LocationManager.PASSIVE_PROVIDER);
		}else{
			if(context instanceof BaseActivity)
			{
				((BaseActivity)context).showMessage(context.getString(R.string.location_services_not_available));
			}
		}
		
	}

	@Override
	public void onLocationChanged(Location location) {
		 lastLocation = location;		
	}

	@Override
	public void onProviderDisabled(String provider) {
				
	}

	@Override
	public void onProviderEnabled(String provider) {
				
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
				
	}

	
	
}
